package com.johnny.springai.multiplatformmodel.controller;

import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatModel;
import com.johnny.springai.multiplatformmodel.option.MultiPlatformModelOptions;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.prompt.ChatOptions;
import org.springframework.ai.deepseek.DeepSeekChatModel;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@RestController
public class MultiPlatformModelController {

    Map<String, ChatModel> platforms = new HashMap<>();

    public MultiPlatformModelController(
            DashScopeChatModel dashScopeChatModel,
            DeepSeekChatModel deepSeekChatModel,
            OllamaChatModel ollamaChatModel) {
        platforms.put("ali", dashScopeChatModel);
        platforms.put("deepseek", deepSeekChatModel);
        platforms.put("ollama", ollamaChatModel);
    }

    /**
     * 多平台多模型调用
     * @param message
     * @param options
     * @return
     *
     * GET http://localhost:8080/chat?message=你好&platform=ollama&model=qwen3:0.6b&temperature=0.8
     * Accept: application/json
     *
     *GET http://localhost:8080/chat?message=你好&platform=ali&model=qwen-plus&temperature=0.8
     * Accept: application/json
     */
    @RequestMapping(value = "/chat",produces = "text/stream;charset=UTF-8")
    public Flux<String> chat(
            String message, MultiPlatformModelOptions options) {

         String platform = options.getPlatform();
         ChatModel chatModel = platforms.get(platform);
        ChatClient.Builder builder = ChatClient.builder(chatModel);

        ChatClient chatClient = builder.defaultOptions(
                ChatOptions.builder()
                        .temperature(options.getTemperature())
                        .model(options.getModel())
                        .build()
        ).build();

        Flux<String> content = chatClient.prompt()
                .user(message)
                .stream().content();
        return content;

    }

}
